<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Overtrue\EasySms\EasySms;



class CommentController extends Controller
{
    
  

    public function store(EasySms $easy_sms,$phone)
    {
           
           
            $code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);

            try {
                $easy_sms->send($phone, [
                    'content'  => "【订单号码】{$code}为您的订单识别码，到店请提供。如非本人操作，请忽略本短信",
                ]);
            } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
                $message = $exception->getException('qcloud')->getMessage();
                dd($message);
            }
    }
    
   
    //添加收藏
    public function create(Request $request)
    {
         $msg = array();
        
        
        $data['open_id'] =  request('open_id','');
        $data['pro_id']  =  request('pro_id','');
        
        $status = DB::table('pro_collection')->where('open_id',$data['open_id'])->where('pro_id',$data['pro_id'])->first();
        
        
        if($status){
                //等于0   并且存在  那么就是收藏  那么就改为取消收藏
                if($status->status ==  0){
                  
                    $where['status'] = 1;
                    $one = DB::table('pro_collection')->where('id',$status->id)->update($where);
                    if($one){
                        
                            $msg = '200';
                       
                            return $msg;
                    }else{
                            $msg = '000';
                         
                            return $msg;
                    }
                }else{
                     $whe['status'] = 0;
                     $two = DB::table('pro_collection')->where('id',$status->id)->update($whe);
                     if($two){
                            $msg = '300';
                          
                            return $msg;
                     }else{
                            $msg = '000';
                           
                            return $msg;
                     }
                     
                }
        }else{
                    $data['status']  =  0;
        
                    $info = DB::table('pro_collection')->insert($data);

                    if($info){
                            $msg = '300';
                           
                            return $msg;
                    }else{
                            $msg = '000';
                           
                            return $msg;
                    }
        }
        
        
    }
    
    //添加评论
    public function content(Request $request)
    {
        
        $data['open_id'] =  request('open_id','');
        $data['pro_id']  =  request('pro_id','');
        $data['content'] =  request('content','');
        $data['status']  = request('status','0');
        $data['created_at']  = date('Y-m-d H:i:s');
        
    
        $info = DB::table('content')->insert($data);
        $msg = array();
        if($info){
                $msg = '200';
                $msg = '评论成功';
                return $msg;
        }else{
                $msg = '000';
                $msg = '评论失败';
                return $msg;
        }
        
        
        
    }
    
    //展示收藏
    public  function showcoll(Request $request,$openid)
    {
        
            $list = \DB::table('pro_collection')
                    ->leftJoin('product','pro_collection.pro_id','=','product.id')
                    ->where('open_id',$openid)
                     ->where('status',0)
                    ->get();
              $msg  = array();
            if(!empty($list)){
                $list = json_decode(json_encode($list),true);
                
                return  $list;
                
            }else{
                $msg = '000';
                $msg = '没有收藏';
                    
                return $msg;
            }
                   
    }
    
    
    public function order(Request $request,$openid)
    {
        
        
        
          $list = \DB::table('order')
                    ->select('order.*','product.pro_name','product.img','product.price','product.y_price','type.name','type.type_img')
                    ->leftJoin('product','order.pid','=','product.id')
                    ->leftJoin('type','product.type_id','=','type.id')
                    ->where('openid',$openid)
                      ->where('status','!=',0)
                    ->get();
              $msg  = array();
            if(!empty($list)){
                $list = json_decode(json_encode($list),true);
                
                return  $list;
                
            }else{
                $msg = '000';
                $msg = '没有订单';
                    
                return $msg;
            }
                   
        
        
    }
    
    
   
}
